package me.shurufa.activities;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import butterknife.Bind;
import de.greenrobot.event.EventBus;
import me.shurufa.R;
import me.shurufa.model.AddDigestBean;
import me.shurufa.model.CameraModel;
import me.shurufa.utils.Constants;
import me.shurufa.widget.smoothcamera.core.CameraManager;

/**
 * 作者 : andy
 * 日期 : 16/3/14 14:19
 * 邮箱 : andyxialm@gmail.com
 * 描述 : 结束添加书摘
 */
public class CompleteAddDigestActivity extends BaseActivity implements View.OnClickListener {

    @Bind(R.id.left_text)
    TextView leftText;

    @Bind(R.id.right_image)
    ImageView rightImage;

    @Bind(R.id.right_text)
    TextView rightText;

    @Bind(R.id.btn_continue_add)
    Button mContinueAddBtn;

    @Bind(R.id.btn_go_my_shelf)
    Button mGoShelfBtn;

    private AddDigestBean mAddDigestBean;
    private Handler mHandler = new Handler();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        activity_layout_res = R.layout.activity_complete_add_digest;
        super.onCreate(savedInstanceState);
        parseArguments();
        initUI();
        initListener();
    }

    public static void initArguments(Intent in, AddDigestBean addDigestBean) {
        in.putExtra(Constants.ARG_ADD_DIGEST, addDigestBean);
    }

    private void parseArguments() {
        this.mAddDigestBean = (AddDigestBean) getIntent().getSerializableExtra(Constants.ARG_ADD_DIGEST);
    }

    private void initListener() {
        mContinueAddBtn.setOnClickListener(this);
        mGoShelfBtn.setOnClickListener(this);
    }

    private void initUI() {
        backInToolbar.setVisibility(View.GONE);
        titleTextView.setText(R.string.digest_preview);
        rightText.setText(getString(R.string.next));
        rightImage.setVisibility(View.GONE);
        rightText.setOnClickListener(this);
        titleTextView.setText(R.string.create_success);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_go_my_shelf:
                onGoMyShelf();
                break;
            case R.id.btn_continue_add:
                onContinueAddDigest();
                break;
        }
    }

    /**
     * 去我的书架
     */
    private void onGoMyShelf() {
        CameraModel.mAddDigestBean = null;
        CameraManager.getInstance().release();

        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                // 清除栈
                Intent intent = new Intent(Constants.EVENT_COMPLETE_ADD_DIGEST);
                EventBus.getDefault().post(intent);

                Intent in = new Intent(CompleteAddDigestActivity.this, NewMainActivity.class);
                in.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                EventBus.getDefault().post(Constants.EVENT_SWITCH_MY_SHELF);
                startActivity(in);
                finish();
            }
        }, 400);

    }

    public void onEventMainThread(String event) {
    }

    /**
     * 继续添加书摘
     */
    private void onContinueAddDigest() {
        // 清除栈
        Intent intent = new Intent(Constants.EVENT_COMPLETE_ADD_DIGEST);
        EventBus.getDefault().post(intent);

        mAddDigestBean.clearToContinueAddDigest();
        CameraModel.mAddDigestBean = mAddDigestBean;

        CameraManager.getInstance().openCamera(this, true);
        finish();
    }

}
